package com.bee.web.push;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/**
 * 极光推送
 *
 * @author liuzy
 * @since 2015年6月2日
 */
public class JPush {

    private String appKey;

    private String masterSecret;

    private Integer maxRetryTimes;

    private JPushClient jPushClient;

    private void init() {
        if (jPushClient == null) {
            jPushClient = new JPushClient(masterSecret, appKey, maxRetryTimes);
        }
    }

    /**
     * 给所有用户推送消息
     *
     * @param message
     * @return
     */
    public String pushAllUser(String title, String message) {
        this.init();
        PushPayload pushPayload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .setAlert(message)
                        .addPlatformNotification(AndroidNotification.newBuilder().setTitle(title).setAlert(message).build())
                        .addPlatformNotification(IosNotification.newBuilder().incrBadge(1).addExtra(title, message).build())
                        .build())
                .build();
        try {
            return jPushClient.sendPush(pushPayload).toString();
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 给指定用户推送消息
     *
     * @param userId
     * @param message
     * @return
     */
    public String pushByUserId(String userId, String title, String message) {
        this.init();
        PushPayload pushPayload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(userId))
                .setNotification(Notification.newBuilder()
                        .setAlert(message)
                        .addPlatformNotification(AndroidNotification.newBuilder().setTitle(title).setAlert(message).build())
                        .addPlatformNotification(IosNotification.newBuilder().incrBadge(1).addExtra(title, message).build())
                        .build())
                .build();
        try {
            return jPushClient.sendPush(pushPayload).toString();
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 给区域内店员推送消息
     *
     * @param areaId
     * @param title
     * @param message
     * @return
     */
    public String pushByAreaId(String areaId, String title, String message) {
        this.init();
        PushPayload pushPayload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.tag(areaId))
                .setNotification(Notification.newBuilder()
                        .setAlert(message)
                        .addPlatformNotification(AndroidNotification.newBuilder().setTitle(title).setAlert(message).build())
                        .addPlatformNotification(IosNotification.newBuilder().incrBadge(1).addExtra(title, message).build())
                        .build())
                .build();
        try {
            return jPushClient.sendPush(pushPayload).toString();
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getAppKey() {
        return appKey;
    }

    public void setAppKey(String appKey) {
        this.appKey = appKey;
    }

    public String getMasterSecret() {
        return masterSecret;
    }

    public void setMasterSecret(String masterSecret) {
        this.masterSecret = masterSecret;
    }

    public Integer getMaxRetryTimes() {
        return maxRetryTimes;
    }

    public void setMaxRetryTimes(Integer maxRetryTimes) {
        this.maxRetryTimes = maxRetryTimes;
    }

}
